package com.learning.platform.repository;

import com.learning.platform.entity.ExamAnswer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface ExamAnswerRepository extends JpaRepository<ExamAnswer, Long> {
    
    /**
     * 查询考试记录的所有答案
     */
    List<ExamAnswer> findByRecordIdOrderByQuestionId(Long recordId);
    
    /**
     * 查询特定题目的答案
     */
    Optional<ExamAnswer> findByRecordIdAndQuestionId(Long recordId, Long questionId);
    
    /**
     * 统计考试记录的答案数量
     */
    long countByRecordId(Long recordId);
    
    /**
     * 统计正确答案数量
     */
    long countByRecordIdAndIsCorrect(Long recordId, Boolean isCorrect);
    
    /**
     * 获取考试记录的总得分
     */
    @Query("SELECT SUM(ea.score) FROM ExamAnswer ea WHERE ea.recordId = :recordId")
    Double getTotalScoreByRecordId(@Param("recordId") Long recordId);
    
    /**
     * 删除考试记录的所有答案
     */
    void deleteByRecordId(Long recordId);
    
    /**
     * 查询需要人工评分的答案
     */
    @Query("SELECT ea FROM ExamAnswer ea WHERE ea.recordId = :recordId AND ea.manualScore IS NULL")
    List<ExamAnswer> findPendingManualGrading(@Param("recordId") Long recordId);
}